﻿using EchoFramework;
using UnityEngine;

/// <summary>
/// 玩家逻辑模块
/// </summary>
public class PlayerLogicModule : LogicModuleBase
{
    public static PlayerLogicModule Instnace
    {
        get { return LogicModuleManager.Instance.GetLogicModule<PlayerLogicModule>(); }
    }

    /// <summary>
    /// 角色坐标
    /// </summary>
    public Coordinate PlayerCoor { get; set; }

    /// <summary>
    /// 角色当前的MapRoadItem
    /// </summary>
    public MapRoadItem PlayerMapRoadItem => MapGenerator.GetMapRoadItem(PlayerCoor);

    private int selfScore;
    /// <summary>
    /// 自身分数
    /// </summary>
    public int Score
    {
        get => selfScore;
        set
        {
            selfScore = value;
            PlayerController?.UpdateScoreTxt();
        }
    }
    
    /// <summary>
    /// 关卡
    /// </summary>
    public int Level { get; private set; }
    
    /// <summary>
    /// 角色控制器
    /// </summary>
    public PlayerController PlayerController { get;  set; }


    public void Init()
    {
        if (PlayerController == null)
        {
            PlayerController = AssetManager.Instance.LoadGameObject<PlayerController>(
                "Assets/GameRes/GamePrefab/Player/Player.prefab");
        }
    }
    
    /// <summary>
    /// 初始化关卡数据
    /// </summary>
    /// <param name="coor">角色坐标</param>
    /// <param name="score">初始分数</param>
    /// <param name="level">关卡</param>
    public void InitLevel(Coordinate coor, int score, int level)
    {
        PlayerCoor = coor;
        Score = score;
        Level = level;
        GameLogger.LogYellow($"关卡初始化 Level:{Level} Score:{Score} Coordinate:{PlayerCoor}");
        PlayerController.transform.localPosition = new Vector3(PlayerCoor.X, PlayerCoor.Y);
        PlayerController.Init();
    }
}